package com.zbdx.controller;

import com.google.gson.Gson;
import com.zbdx.dao.CarDao;
import com.zbdx.dao.impl.CarDaoImpl;
import com.zbdx.domain.Resultdata;
import com.zbdx.domain.productVo;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author ghp
 * @Creat 2024/9/18 10:24
 * @Version 1.0
 * Description:
 */

@WebServlet("/shopping/car/selectGoodsByUsername")
public class seletServlet extends HttpServlet{
    private CarDao carDao = new CarDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
//        //1.设置编码
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
//        //2.获取请求参数
//        String method = req.getParameter("method");
//        System.out.println("method = "+method);
//        if("selectGoodsByUsername".equals(method)){
//            try {
//                this.selectGoodsByUsername(req,resp);
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//        }

        resp.setContentType("application/json");
        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("user");
        List<productVo> goodsList = null;
            try {
            goodsList = carDao.selectGoodsByUsername(username);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        Resultdata resultdata = new Resultdata(0,"成功",goodsList);
        // 使用Gson的Gson对象将对象转换为JSON字符串
        Gson gson = new Gson();
        String jsonStr = gson.toJson(resultdata);

        // 设置响应内容类型为JSON
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");

        // 将JSON字符串写入响应
        PrintWriter out = resp.getWriter();
            out.print(jsonStr);
            out.flush();
    }

    /**
     * 根据用户名查询购物车中的全部商品
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    public void selectGoodsByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("user");
        List<productVo> goodsList = carDao.selectGoodsByUsername(username);
        req.setAttribute("goodsList", goodsList);

        // 请求转发到jsp页面
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("shopcar.jsp");
        requestDispatcher.forward(req, resp);
    }
//    resp.setContentType("application/json");
//    HttpSession session = req.getSession();
//    String username = (String) session.getAttribute("user");
//    List<productVo> goodsList = null;
//        try {
//        goodsList = carDao.selectGoodsByUsername(username);
//    } catch (SQLException e) {
//        e.printStackTrace();
//        throw new RuntimeException(e);
//    }
//
//    // 使用Gson的Gson对象将对象转换为JSON字符串
//    Gson gson = new Gson();
//    String jsonStr = gson.toJson(goodsList);
//
//    // 设置响应内容类型为JSON
//        resp.setContentType("application/json");
//        resp.setCharacterEncoding("UTF-8");
//
//    // 将JSON字符串写入响应
//    PrintWriter out = resp.getWriter();
//        out.print(jsonStr);
//        out.flush();

//    public void submitCount(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
//        HttpSession session = req.getSession();
//        String username = (String) session.getAttribute("user");
//        List<productVo> goodsList = carDao.selectGoodsByUsername(username);
//        req.setAttribute("goodsList", goodsList);
//
//    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
